package in.muton.edushare.poll.answers.individual;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.PollQuestions;
import com.edushare.common.vo.Users;

public class FreeFormSingleAns extends EdusharePanel {
	private static final long serialVersionUID = 7812319195693608865L;
	private IModel<PollQuestions> question;
	private Integer rows = 5;
	private String str = "hello";
	private PageableListView<IModel<Users>> plv;
	public static final Log log = LogFactory.getLog(FreeFormSingleAns.class);

	public FreeFormSingleAns(String id, IModel<PollQuestions> model) {
		super(id);
		question = model;
		log.debug("Constructor being called");
		add(JavascriptPackageResource.getHeaderContribution(
				FreeFormSingleAns.class, "FreeFormSingleAns.js"));
		init();
	}

	private void init() {
		Form<Void> f1 = new Form<Void>("userSearch");
		f1.add(new TextField<String>("user", new PropertyModel<String>(this,
				"sortBy")));

		Form<Void> f2 = new Form<Void>("setRows");
		f2.add(new TextField<Integer>("rows", new PropertyModel<Integer>(this,
				"rows")));
		add(f1);
		add(f2);

		WebMarkupContainer wmc = new WebMarkupContainer("ajaxWMC");
		wmc.setOutputMarkupId(true);
		wmc.setMarkupId("ajaxWMC");
		wmc.setVersioned(false);
		add(wmc);
		wmc.add(plv = new PageableListView<IModel<Users>>("ajaxPLV", new UserModel(), getRows()) {
			@Override
			protected void populateItem(ListItem<IModel<Users>> arg0) {
				Users u = arg0.getModelObject().getObject();
				String ans = new UserPollDAO(getORMInterface())
						.getStringAnswer(u, question.getObject());
				arg0.add(new EduShareURLPanel("submitter", arg0
						.getModelObject()));
				arg0.add(new MultiLineLabel("answer", ans));
			}

			private static final long serialVersionUID = -192427948521508463L;
		});
		wmc.add(new EdushareAjaxPagingNavigation("navigator", plv));
	}

	private class UserModel extends InnerList<Users> {
		@Override
		public List<IModel<Users>> getObject() {
			UserPollDAO upDAO = new UserPollDAO(getORMInterface());
			PollQuestions pq = question.getObject();
			List<Users> uL = upDAO.getPollRespondents(pq.getPoll());
			List<Users> filteredList = new ArrayList<Users>();
			for (Users u : uL) {
				String ans = upDAO.getStringAnswer(u, pq);
				if (StringUtil.isValid(ans)) {
					filteredList.add(u);
				}
			}
			return new CayenneList<Users>(filteredList).getObject();
		}

		private static final long serialVersionUID = 7559546695529194902L;
	}

	public String getSortBy() {
		log.debug("Return sort by = " + str);
		return new String(str);
	}

	public void setSortBy(String in) {
		str = new String(in);
		log.debug("Got sort by = " + str);
	}

	public void setRows(Integer rows) {
		this.rows = rows;
		plv.setRowsPerPage(rows);
		log.debug("Got rows = " + rows);
	}

	public Integer getRows() {
		log.debug("Got rows = " + rows);
		return rows;
	}
}
